package com.itheima.controller;




import com.itheima.entity.Result;
import com.itheima.utils.ValidateCodeUtils;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.Properties;

@RestController
@RequestMapping("/email")
public class EmailController {

    //引入jeids连接池,用于存储验证码
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/sendEmail")
    public Result sendEmail(String email) throws MessagingException, GeneralSecurityException {
        try {
            //创建一个配置文件并保存
            Properties properties = new Properties();

            properties.setProperty("mail.host","smtp.qq.com");

            properties.setProperty("mail.transport.protocol","smtp");

            properties.setProperty("mail.smtp.auth","true");


            //QQ存在一个特性设置SSL加密
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            properties.put("mail.smtp.ssl.enable", "true");
            properties.put("mail.smtp.ssl.socketFactory", sf);

            //创建一个session对象
            Session session = Session.getDefaultInstance(properties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("857259723@qq.com","ytazkjyqiwnlbebg");
                }
            });

            //开启debug模式
            session.setDebug(true);

            //获取连接对象
            Transport transport = session.getTransport();

            //连接服务器
            transport.connect("smtp.qq.com","857259723@qq.com","ytazkjyqiwnlbebg");

            //创建邮件对象
            MimeMessage mimeMessage = new MimeMessage(session);

            //邮件发送人
            mimeMessage.setFrom(new InternetAddress("857259723@qq.com"));

            //邮件接收人
            mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email));

            //邮件标题
            mimeMessage.setSubject("传智健康注册邮箱激活码");

            // 随机生成的邮箱激活码
            Integer integer = ValidateCodeUtils.generateValidateCode(6);

            //邮件内容
            mimeMessage.setContent("邮箱激活码为:"+integer ,"text/html;charset=UTF-8");

            // 存入redis中
            jedisPool.getResource().setex(email,300,integer.toString());

            //发送邮件
            transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());

            //关闭连接
            transport.close();

            return new Result(true,"邮箱验证码发送成功");
        }  catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"邮箱验证码发送失败");
        }
    }
}
